<?

$_table = 'data';
$_menu = D_MENU;
$_values	= array('name', 'value', 'text', 'attr', 'tag', 'assoc');	// which fields are stored
$_order = 'id_index';

$DATA['obj'] = datafetch_array('db_list', array('table' => tb('data'), 
	'where' => "id_data=$_menu AND id_parent=0 AND (assoc IS NULL OR assoc IN ('','".implode("','", array_keys($DATA['langs']))."'))", 
	'index' => 'id', 'order' => 'name'));

if (($obj = (int)param('edit')) && isset($DATA['obj'][$obj])) {
	
	unset($DATA['obj'][$obj]);

	$_crit = "id_data=$_menu AND id_parent=$obj";
	


	if ($a = (int)param('delete')) {
		
		dataset('db_delete', tb('data'), "$_crit AND id=$a");
	
//		header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'delete')));
		exit;
	}
	
	if ($a = (int)param('show')) {
		
		dataset('db_update', tb($_table), "attr = ".dbOr('attr', AA_VISIBLE), "$_crit AND (id=$a)");
	
//		header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'show')));
		exit;
	}
	
	if ($a = (int)param('hide')) {
		
		dataset('db_update', tb($_table), "attr = ".dbAnd('attr', ~AA_VISIBLE), "$_crit AND (id=$a)");
	
//		header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'hide')));
		exit;
	}
	
	
	if ($a = strtolower(trim(param('sort')))) {
		
		switch($a) {
		
			case 'manual':	// ajax request, no data should be returned
	
				$t = 1;
				
				parse_str(postdata('order'), $temp);
				$DATA['objects'] = datafetch_array('db_list', array('table' => tb('data'), 'where' => $_crit, 'index' => 'id', 'order' => $_order));

				if (is_array($temp = $temp['objects']) && sizeof($temp)) {
	
					foreach($temp as $id) {
						if ($id = (int)$id) {
							if ($DATA['objects'][$id]) {
								dataset('db_update', tb($_table), "$_order = ".$t++, "$_crit AND id=$id");
								unset($DATA['objects'][$id]);
							}
						}
					}
					
					if (sizeof($DATA['objects']))
						foreach($DATA['objects'] as $id => $v) 
							dataset('db_update', tb($_table), "$_order = ".$t++, "$_crit AND id=$id");
				}
				
				exit;	// ajax: no more processing needed
				break;
	}
		
		header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'sort')));
		exit;
	}
	
	
	if ($a = strtolower(trim(param('add')))) {
		
		switch($a) {
		
			case 'item':	// ajax request, no data should be returned

				if ($title = h(trim(postdata('title')))) {
					$max = ($m = datafetch('db_item', tb('data'), $_crit, 'MAX(id_index) as m')) ? $m['m']+1 : 1;
					
					if ($id = datafetch('db_insert', tb('data'), 
						array('id_data' => $_menu, 'name' => $title, 'id_parent' => $obj, 'id_index' => $max))) {
							
						print php2js(array(
							'itemId' => $id, 
							'itemAttr' => 0, 
							'itemTag' => 0, 
							'itemName' => $title, 
							'itemValue' => '', 
							'itemLang' => '', 
							'itemText' => ''));
					}
					
				}

				exit;	// ajax: no more processing needed
				break;
		}
	}
	
	
	
	switch (action(array('create_object', 'save_objects'))) {
	
	
		case 'create_object':
			
			if ($title = trim(postdata('title'))) {
	
				$max = ($m = datafetch('db_item', tb('data'), $_crit, 'MAX(id_index) as m')) ? $m['m']+1 : 1;
					
				if ($id = datafetch('db_insert', tb('data'), array('id_data' => $_menu, 'name' => $title, 'id_parent' => $obj, 'id_index' => $max))) {
					
		
					// redirect to edit page
//					header("Location: edit-$id/");
//					exit;
				}
					// !!! else error on insert - handle it
			}
			
			break;
			
		case 'save_objects':
			if (is_array($keys = postdata('name')) && sizeof($keys)) { 

				$_data = array();
				$_index = 0;
				$_pk = array();
				foreach($keys as $id => $v) if ((int)$id) {
					$_pk[] = $id;
					$vals = array();
					foreach($_values as $nm)
						$vals[$nm] = trim($_POST[$nm][$id]);
					$_data[] = array_merge(array('id' => $id, 'id_data' => $_menu, 'id_parent' => $obj, 'id_index' => ++$_index), $vals);
				}

				dataset('db_delete', tb('data'), 'id in('.implode(',', $_pk).')');
				dataset('db_insert', tb('data'), $_data);

				header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'edit')));
				exit;

			}
			
			break;
			
	
	}
		
	
	$DATA['objects'] = datafetch_array('db_list', array('table' => tb('data'), 'where' => $_crit, 'index' => 'id', 'order' => 'id_index'));

}